package com.czx.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czx.dto.UserDto;
import com.czx.entity.User;
import com.czx.page.PageData;
import com.czx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.util.Arrays;
import java.util.Date;


@RestController
@RequestMapping("/sys/user")
public class sysUserController {
    @Autowired
    private UserService userService;
    @GetMapping("/list")
    public PageData<User> userList(Page page){
        Page userPage = userService.page(page);
        PageData pageData = new PageData<>(userPage.getRecords(), userPage.getTotal());
        return pageData;
    }
    /**
     * 根据id查询
     *
     */
    @GetMapping("/userId/{id}")
    public UserDto getUserById(@PathVariable("id") Integer id){
        System.out.println(id+"00000000000000000000000");
        //交给业务层处理
        User user =userService.getUserById(id);
        UserDto userDto=new UserDto();
        userDto.setId(user.getId());
        //将得到的对象返回
        return userDto;


    }
    @PostMapping
    public Boolean add(@RequestBody UserDto userDto){
        System.out.println(userDto+"7777777777777777777");
        User user=new User();
        user.setUsername(userDto.getUsername());
        user.setType(userDto.getType());
        user.setStatus(userDto.getStatus());
        user.setEmail(userDto.getEmail());
        user.setCreateTime(new Date());
        boolean save = userService.save(user);
        return save;
    }
    @PutMapping
    public Boolean update(@RequestBody UserDto userDto){
        User user=new User();
        user.setId(userDto.getId());
        user.setType(userDto.getType());
        user.setStatus(userDto.getStatus());
        user.setEmail(userDto.getEmail());
        user.setUsername(userDto.getUsername());
        user.setCreateTime(new Date());
        boolean result=userService.updateById(user);
        return result;
    }
    @DeleteMapping
    public Boolean delete(@RequestParam("ids") Long[] ids){
        return userService.removeBatchByIds(Arrays.asList(ids));
    }



}
